package com.admission.service;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.admission.dao.CollegeProvinceDao;
import com.admission.model.CollegeProvince;
import com.admission.model.ProvinceForm;
import com.admission.util.ArrayUtils;
import com.admission.util.DeleteFile;
import com.admission.util.EnrollUtils;
import com.admission.util.ExcelUtils;
import com.admission.util.XmlUtils;

@Repository
@Transactional
public class CollegeProvinceManager {
	private CollegeProvinceDao collegeProvinceDao;
	private ProvinceFormManager provinceFormManager;
	private ArrayUtils arrayUtils;

	/**
	 * 保存
	 * 
	 * @throws IOException
	 */
	public void save(String excelPath, String CollegeId) throws Exception {
		Workbook workbook = ExcelUtils.getCommonWorkbook(excelPath);
		Sheet sheet = workbook.getSheetAt(0);
		ProvinceForm c = provinceFormManager.getById(CollegeId);
		for (int j = 1; j <= sheet.getLastRowNum(); j++) {
			CollegeProvince rd = new CollegeProvince();
			Row row1 = sheet.getRow(j);
			rd.setProvince((String) arrayUtils.getKey(
					arrayUtils.getProvinceMap(),
					EnrollUtils.getCellFormatValue(row1.getCell(0))));
			if (StringUtils.isEmpty(rd.getProvince())) {
				throw new Exception("高考录取公告表格第" + (j + 1)
						+ "行的省份有错误，高考录取公告表格提交失败");
			}
			rd.setContent(EnrollUtils.getCellFormatValue(row1.getCell(1)));
			rd.setProvinceForm(c);
			collegeProvinceDao.save(rd);
			try {
				XmlUtils.Create(rd);
			} catch (Exception e) {
				throw new Exception(
						"高考录取公告页面生成失败·，可能数据库中存在不合法字段，具体情况，请联系开发人员调试"
								+ e.toString());
			}
		}
		// DeleteFile.delete(excelPath);
	}

	/**
	 * 
	 * 根据collegeFrom id批量删除
	 * 
	 * @param ids
	 */
	public void deleteByIds(String id) {
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/WEB-INF/xml");
		List<CollegeProvince> l = collegeProvinceDao.findBy("provinceForm.id",
				id);
		for (CollegeProvince c : l) {
			String regx = "collegeprovince-" + c.getProvince() + ".xml";
			File file = new File(serverrealPath + File.separator + regx);
			DeleteFile.deleteByFile(file);
			collegeProvinceDao.delete(c);
		}
	}

	public List<CollegeProvince> getList() {
		return collegeProvinceDao.getAll();
	}

	public CollegeProvinceDao getCollegeProvinceDao() {
		return collegeProvinceDao;
	}

	@Autowired
	public void setCollegeProvinceDao(CollegeProvinceDao collegeProvinceDao) {
		this.collegeProvinceDao = collegeProvinceDao;
	}

	public ProvinceFormManager getProvinceFormManager() {
		return provinceFormManager;
	}

	@Autowired
	public void setProvinceFormManager(ProvinceFormManager provinceFormManager) {
		this.provinceFormManager = provinceFormManager;
	}

	public ArrayUtils getArrayUtils() {
		return arrayUtils;
	}

	@Autowired
	public void setArrayUtils(ArrayUtils arrayUtils) {
		this.arrayUtils = arrayUtils;
	}
}
